﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 12/17/2009
 * Time: 9:18 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Contact.
	/// </summary>
	
	public class Contact : csharp.common.DefaultClass, csharp.common.IClass
	{
		private string _person_id;
		private string _person_name;
		private string _phone_first;
		private string _phone_second;
		private string _phone_third;
		private string _person_email;
		private string _person_born_date;
		
		public Contact()
		{
			clear();	
		}
		
		#region Declarations methods		
		public string name {
			get { return _person_name; }
			set { _person_name = value; }
		}
		
		public string phonefirst {
			get { return _phone_first; }
			set { _phone_first = value; }
		}
		
		public string phonesecond {
			get { return _phone_second; }
			set { _phone_second = value; }
		}
		
		public string phonethird {
			get { return _phone_third; }
			set { _phone_third = value; }
		}
		
		public string email {
			get { return _person_email; }
			set { _person_email = value; }
		}
		
		public string born_date{
			get { return _person_born_date;}
			set { _person_born_date = value;}
		}
		#endregion
		
		
		public void clear()
		{
			_person_id        = String.Empty;
			_person_name      = String.Empty;
			_person_name      = String.Empty;
			_phone_first      = String.Empty;
			_phone_second     = String.Empty;
			_phone_third      = String.Empty;
			_person_email     = String.Empty;
			_person_born_date = String.Empty;
		}
		
		public override bool delete()
		{
			return base.delete();
		}
		
		public override void get(System.Data.DataRow dr)
		{
			_person_id        = dr["person_id"].ToString();
			_person_name      = dr["person_name"].ToString();
			_phone_first      = dr["phone1"].ToString();
			_phone_second     = dr["phone2"].ToString();
			_phone_third      = dr["phone3"].ToString();
			_person_email     = dr["person_email"].ToString();
			_person_born_date = Convert.ToDateTime(dr["person_born_date"]).ToString("dd/MM/yyyy");
		}
		
		public override bool save()
		{
			return base.save();
		}
		
		public override void setGsql()
		{
			base.setGsql();
		}
		
		public override bool store(csharp.common.GenerateSql.TCommandType ctype)
		{
			return base.store(ctype);
		}
		
		public override bool update()
		{
			return base.update();
		}
		
	}
	public class Contacts : csharp.common.DefaultList, csharp.common.IList
	{
		public Contacts()
		{
			
		}
				
		private void mountSql()
		{
			gsql.sql = "SELECT DISTINCT person_id, person_name, person_email, person_born_date, "
                       +"(SELECT phone_number " 
				       +"FROM common.phone "
				       +"WHERE phone_person_id = person_id LIMIT 1 OFFSET 0) AS phone1, "
				       +"(SELECT phone_number " 
				       +"FROM common.phone " 
				       +"WHERE phone_person_id = person_id LIMIT 1 OFFSET 1) AS phone2, "
				       +"(SELECT phone_number " 
				       +"FROM common.phone " 
				       +"WHERE phone_person_id = person_id LIMIT 1 OFFSET 2)  AS phone3 "             
				       +"FROM common.person LEFT JOIN common.phone ON person_id = phone_person_id AND "
			           +"person_company_id = "+Information.companyId;			
		}
		
		public bool fillList()
		{
			mountSql();
			gsql.sql+=" ORDER BY person_name";
			
			return mountList();
		}
		
		public bool searchByName(string value)
		{
			int tam = items.Count;
			value = value.ToUpper();
			for(int i = 0; i < tam; i++)
			{
				if ( ((Contact)items[i]).name.ToUpper().StartsWith(value))
				{
					index= i;
					return true;
				}
			}
			return false;
		}
		
		public bool searchByPhone(string value)
		{
			
			int tam = items.Count;
			
			for (int i=0; i < tam; i++)
			{
				if (((Contact)items[i]).phonefirst.StartsWith(value) || 
				    ((Contact)items[i]).phonesecond.StartsWith(value) ||
				    ((Contact)items[i]).phonethird.StartsWith(value))
				{
					index= i;
					return true;
				}
			}
			return false;
		}
		
		public Contact get()
		{
			return (Contact)items[index];
		}
		
		public Contact get(Int32 index)
		{
			return (Contact)items[index];
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.executeSql())
				return false;
			
			items.Clear();
			tam= gsql.getRowCount();
			for (int i=0; i < tam; i++)
			{
				Contact c = new Contact();
				c.get(gsql.getRow(i));
				items.Add(c);
			}
			return true;
		}
		
		public bool fillListByBirthdayDay(string day, string month)
		{
			mountSql();
			gsql.sql+= "WHERE EXTRACT(DAY FROM person_born_date) = "+day
				    + " AND EXTRACT(MONTH FROM person_born_date) = "+month
			        + " AND person_company_id = "+Information.companyId
				    + " AND NOT person_juridic "
				    + " ORDER BY person_name";
			
			return mountList();
		}	
		
		public bool fillistByBirthdayMonth(string value)
		{
			mountSql();
			gsql.sql+= "WHERE EXTRACT(MONTH FROM person_born_date) ="+value
					+ " AND person_company_id = "+Information.companyId
				    + " AND NOT person_juridic "
					+ " ORDER BY person_name";
			
			return mountList();
		}		
		
		public bool fillistByBirthdayAll()
		{
			mountSql();
			gsql.sql+= "WHERE person_company_id = "+Information.companyId
				    + " AND NOT person_juridic "
					+ " ORDER BY person_name";
			
			return mountList();
		}		
	}
}
